package com.sxwl.view;

import com.sxwl.entity.*;
import com.sxwl.utils.P;
import com.sxwl.utils.PathUtlis;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

/**
 * @author reading
 * @time ：2019/11/3 22:42
 * @Description :游戏界面
 */
public class GamePanel extends JPanel {
    public static long count = 0;
    public boolean flag = true;
    public static ArrayList<EnemyPlane> enemyPlanes = new ArrayList<>();
    public ImageIcon bz2 = new ImageIcon(PathUtlis.getPath("images/dd2.png"));
    private FighterPlane fighterPlane;
    ArrayList<Particles> bullets;

    public GamePanel(FighterPlane fighterPlane, ArrayList<Particles> bullets) {
        this.fighterPlane = fighterPlane;
        this.bullets = bullets;
        new updateView().start();
    }

    /**
     * 画飞机
     *明显有问题啊，三个连续放到一起，简直有病
     * @param graphics the <code>Graphics</code> context in which to paint
     */
    @Override
    public void paint(Graphics graphics) {
        graphics.setColor(Color.black);
        graphics.fillRect(0, 0, P.FRAME_WIDTH, P.FRAME_WIDTH);
        graphics.setColor(Color.red);
        graphics.drawString("杀敌数量:" + count, 650, 50);
        //子弹
        for (Particles bullet : bullets) {
            bullet.drawMe(graphics);
        }
        //画出战斗机
        if (fighterPlane != null && fighterPlane.left > 0) {
            fighterPlane.drawMe(graphics);
        } else {

        }
        //画出敌机
        try {
            drawEnemys(graphics);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 画出敌机
     *
     * @param graphics
     * @description 画的慢一点
     */
    public void drawEnemys(Graphics graphics) throws InterruptedException {
        //画出所以敌机
        for (int i = 0; i < enemyPlanes.size(); i++) {
            //判断敌机和战斗机是否发生碰撞
            if (fighterPlane.getRect().intersects(enemyPlanes.get(i).getRect())) {
                fighterPlane.left = 0;
                //g.drawImage(bz2.getImage(),(int)arrayList.get(i).x,(int)arrayList.get(i).y,150,150,null);
                graphics.drawImage(bz2.getImage(), (int) fighterPlane.x, (int) fighterPlane.y, 150, 150, null);
                //停止
                flag = false;
            }
            if (enemyPlanes.get(i).y >= 800) {
                int c = new Random().nextInt(650) + 50;
                enemyPlanes.get(i).x = c;
                enemyPlanes.get(i).y = -150;
            }
            //画出敌机，怎么让绘制的时间变长呢？
            if (enemyPlanes.get(i).feft <= 0) {
                Thread.sleep(5);
                enemyPlanes.get(i).drawMe(graphics);
            } else {
                int x = (int) enemyPlanes.get(i).x, y = (int) enemyPlanes.get(i).y;
                Thread.sleep(5);
                graphics.drawImage(bz2.getImage(), x, y, 150, 150, null);
                enemyPlanes.get(i).feft--;
                if (enemyPlanes.get(i).feft == 1) {
                    int c = new Random().nextInt(650) + 50;
                    enemyPlanes.get(i).x = c;
                    enemyPlanes.get(i).y = -150;
                    GamePanel.count++;
                }
            }
        }
    }

    /**
     * 刷新界面
     */
    class updateView extends Thread {
        @Override
        public void run() {
            while (flag) {
                repaint();
            }
        }
    }

}
